package com.example.onlineeducation.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.onlineeducation.base.BaseViewModel
import com.example.onlineeducation.data.entity.ExamEntity
import com.example.onlineeducation.data.entity.ProInfoEntity
import com.example.onlineeducation.data.entity.TopicEntity
import com.example.onlineeducation.data.json.ExamJson
import com.example.onlineeducation.repository.MainTopicRepository
import com.yc.jetpacklib.net.YcResult
import com.yc.jetpacklib.net.doFail
import com.yc.jetpacklib.net.doSuccess
import kotlinx.coroutines.flow.collectLatest

class MainTopicVM : BaseViewModel() {
    private val mMainTopicRepository: MainTopicRepository by lazy { MainTopicRepository(mApiService) }

    private val _mTopicListDataModel = MutableLiveData<YcResult<List<TopicEntity>?>>()
    val mTopicListDataModel: LiveData<YcResult<List<TopicEntity>?>> = _mTopicListDataModel
    fun getTopicList(projectId: String, isPass: String) = ycLaunch {
        mMainTopicRepository.getTopicList(projectId, isPass).collectLatest {
            _mTopicListDataModel.postValue(it)
        }
    }

    private val _mTopicListForSearchDataModel = MutableLiveData<YcResult<List<TopicEntity>?>>()
    val mTopicListForSearchDataModel: LiveData<YcResult<List<TopicEntity>?>> =
        _mTopicListForSearchDataModel

    fun getTopicForSearchList(projectId: String, keyName: String) = ycLaunch {
        mMainTopicRepository.getTopicForNameList(projectId, keyName).collectLatest {
            _mTopicListForSearchDataModel.postValue(it)
        }
    }


    private val _mExamListDataModel = MutableLiveData<YcResult<List<ExamEntity>?>>()
    val mExamListDataModel: LiveData<YcResult<List<ExamEntity>?>> = _mExamListDataModel
    fun getExamList(projectId: String, groupId: String) = ycLaunch {
        mMainTopicRepository.getExamList(projectId, groupId).collectLatest {
            _mExamListDataModel.postValue(it)
        }
    }

    private val _mExamListForSearchDataModel = MutableLiveData<YcResult<List<ExamEntity>?>>()
    val mExamListForSearchDataModel: LiveData<YcResult<List<ExamEntity>?>> =
        _mExamListForSearchDataModel

    fun getExamForSearchList(projectId: String, groupId: String) = ycLaunch {
        mMainTopicRepository.getExamForNameList(projectId, groupId).collectLatest {
            _mExamListForSearchDataModel.postValue(it)
        }
    }

    private val _mCheckStudyTimeDataModel = MutableLiveData<YcResult<ExamEntity>>()
    val mCheckStudyTimeDataModel: LiveData<YcResult<ExamEntity>> = _mCheckStudyTimeDataModel
    fun getCheckStudyTime(projectId: String, topicId: String, mExamEntity: ExamEntity) = ycLaunch {
        mMainTopicRepository.getCheckStudyTime(projectId, topicId).collectLatest {
            it.doSuccess {
                _mCheckStudyTimeDataModel.postValue(
                    YcResult.Success(mExamEntity)
                )
            }.doFail {
                _mCheckStudyTimeDataModel.postValue(
                    YcResult.Fail(it)
                )
            }
        }
    }


}
